/**
 * @author:fangxiaohua
*  @邮箱：abc2710712@qq.com
*  @qq:1295168875
*   用户业务操作操作
*/
void(function(exports){
	//数据库操作
	var dbLib = require('../lib/dbLib');
	//工具类模块
	var tools = require('../lib/toolsLib');
	//用户常量配置
	var userConfig = require('../lib/contantsLib').USER_CONFIG;
	
	//登录操作
	exports.login = function(req, res){
		console.log("===================="+req.body);
		var account=req.body. account;
		//密码des加密
		var password=tools.DES.encryptDES(req.body.password);
		console.log("==="+password+"====") ;
		var sql="select u.*,GROUP_CONCAT(r.id) as roleId ,GROUP_CONCAT(r.name) as roleName from t_user u LEFT JOIN t_user_role ur on u.id=ur.usr_id LEFT JOIN t_role r on ur.role_id=r.id where u.account=? and u.password=? group by u.id";
		var parame=[account,password];
		//查询用户
		dbLib.dbQuery(sql,parame,function(err,result){
			if(err){
				res.send(userConfig.login_status.error);
				return ;
			}
			else if(!result[0]){
				res.send(userConfig.login_status.user_error);
				return ;
			}
			//账号是否已被管理员禁用判断
			else if(result[0].status==0){
				res.send({"status":3,"info":result[0].reason});
				return ;
			}
			//将用户信息存入session
		 	req.session.user=result[0];
			//更新最后登录时间
		 	var id = result[0].id;
			 sql="update t_user au set au.last_login_time=now() where au.id=?";
			 parame=[id];
			 dbLib.dbUpdate(sql,parame,function(err,result){
				 	if(err){
				 		res.send(userConfig.login_status.error);
				 		return ;
				 	}
				 	res.send(userConfig.login_status.success);
				 	return ;
			 });
		 });
	};

	//注册
	exports.register=function(req, res){
		var parms=req.body;
		var parame=[parms.account];
		var sql="select u.id from t_user u where u.account=?";
		dbLib.dbQuery(sql,parame,function(err,result){
			if(err){
				res.send(userConfig.register_status.error);
		 		return ;
			}
			if(result[0]){
				res.send(userConfig.register_status.account_repeat);
		 		return ;
			}
			
			var password=tools.DES.encryptDES(req.body.password);
			parame=[parms.account,password,parms.nick,parms.telephone,parms.avatar,parms.sex];
			sql="insert into t_user(account,password,name,telephone,last_login_time,avatar,sex) values(?,?,?,?,now(),?,?)";
			dbLib.dbAdd(sql,parame,function(err,result){
				 	if(err){
				 		res.send(userConfig.register_status.error);
				 		return ;
				 	}
				 	//查询新添用户
				 	var usrId=result.insertId;
				 	//分配一般用户角色
					parame=[usrId];
					//3表示的是一般用户
					sql="insert into t_user_role(usr_id,role_id)values(?,3)";
				 	dbLib.dbAdd(sql,parame,function(err,result){
				 		if(err){
				 			res.send(userConfig.register_status.error);
					 		return ;
				 		}
					 	sql="select u.*,GROUP_CONCAT(r.id) as roleId ,GROUP_CONCAT(r.name) as roleName from t_user u LEFT JOIN t_user_role ur on u.id=ur.usr_id LEFT JOIN t_role r on ur.role_id=r.id where u.id=?  group by u.id"
					 	dbLib.dbQuery(sql,parame,function(err,result){
					 		//将用户信息存入session
						 	req.session.user=result[0];
					 		res.send(userConfig.register_status.success);
					 		return ;
					 	});
				 	});
			});
		});
	};
	
	//密码找回
	exports.findpwd=function(req, res){
		var account=req.body. account;
		var telephone=req.body. telephone;
		var sql="select password from t_user u where u.account=? and u.telephone=?";
		var parame=[account,telephone];
		dbLib.dbQuery(sql,parame,function(err,result){
				if(err){
					res.send({"infos":userConfig.findpwd_status.error,"pwd":""});
					return ;
				}
				if(result[0]){
					var password=tools.DES.decryptDES(result[0].password);
					res.send({"infos":userConfig.findpwd_status.success,"pwd":password});
					return ;
				}
				res.send({"infos":userConfig.findpwd_status.info_error,"pwd":""});
		});
	};
	
	//登出
	exports.loginout=function(req, res){
		req.session.destroy(function(err){
			res.redirect('login');
		});
	};
	
	//获取用户详细信息
	exports.getProfile=function(req,res){
		var sql="select * from t_user u where u.id=?";
		var parame=[req.session.user.id]
		dbLib.dbQuery(sql,parame,function(err,result){
			res.send(result[0]);
		});
	}
	
	//更新用户信息
	exports.updateProfile=function(req,res){
		var sql=["update t_user u set  "]
		switch (req.body.name) {
			case "name":{sql.push(" u.name=? ");break;}
			case "sex":{sql.push(" u.sex=? ");break;}
			case "age":{sql.push(" u.age=? ");break;}
			case "birthday":{sql.push(" u.birthday=? ");break;}
			case "telephone":{sql.push(" u.telephone=? ");break;}
			case "location":{sql.push(" u.location=? ");break;}
			case "about":{sql.push(" u.about=? ");break;}
			case "avatar":{sql.push(" u.avatar=? ");break;}
		}
		sql.push(" where u.id=? ");
		var parame=[req.body.value,req.session.user.id]
		dbLib.dbUpdate(sql.join(''),parame,function(err,result){
			if(err){
				res.send("false");
				return ;
			}
			sql="select u.*,GROUP_CONCAT(r.id) as roleId ,GROUP_CONCAT(r.name) as roleName from t_user u LEFT JOIN t_user_role ur on u.id=ur.usr_id LEFT JOIN t_role r on ur.role_id=r.id where  u.id=?";
			parame=[req.session.user.id];
			dbLib.dbQuery(sql,parame,function(err,result){
				if(err){
					res.send("false");
					return ;
				}
				req.session.reload(function(err) {
					req.session.user=result[0];
				});
			});
			res.send("true");
		});
	};
	
	//更新用户密码
	exports.updatePassword=function(req,res){
		var npwd=req.body.newPwd;
		var opwd=tools.DES.encryptDES(req.body.oldPwd);
		var pwd=req.session.user.password;
		if(opwd!=pwd){
			res.send(userConfig.editpwd_status.oldpwd_error);
			return;
		}
		var sql="update  t_user u set u.password=? where u.id=?";
		var parame=[npwd,req.session.user.id]
		dbLib.dbUpdate(sql,parame,function(err,result){
			if(err){
				res.send(userConfig.editpwd_status.error);
				return ;
			}
			res.send(userConfig.editpwd_status.success);
		});
		
	};
})( (function(){
    if(typeof exports === 'undefined') {
        window.exports = {};
        return window.exports;
    } else {
        return exports;
    }
})());